﻿namespace AmazonS3PublishPlugin.Helpers
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Collections.Generic;

    public static class Helper
    {
        public static BitmapSource CaptureScreen(Visual target, double dpiX, double dpiY)
        {
            if (target == null)
            {
                return null;
            }

            Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
            var rtb = new RenderTargetBitmap(
                (int)(bounds.Width * dpiX / 96.0),
                (int)(bounds.Height * dpiY / 96.0),
                dpiX,
                dpiY,
                PixelFormats.Pbgra32);
            var dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                var vb = new VisualBrush(target);
                ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
            }

            rtb.Render(dv);
            return rtb;
        }

        public static long GetTotalSize(IEnumerable<string> files)
        {
            return files == null ? 0 : files.Sum(file => new FileInfo(file).Length);
        }
    }
}